import * as types from "./mutations-types";
import server from "@/api/server/index";
import { Toast } from "mint-ui";
import { setUserId, setToken, getToken } from "@/common/js/cookie";
import { ArrIsObject, clone } from "@/common/js/util";
/**
 * 登录
 * */
export const login = function({ commit, state }, user) {
  return new Promise((resolve, reject) => {
    server
      .login(user.phone, user.password)
      .then(rs => {
        commit(types.SET_USER_ID, rs.user.id);
        commit(types.SET_TOKEN, rs.token);
        setUserId(rs.user.id);
        setToken(rs.token);
        resolve(rs);
      })
      .catch(ms => {
        reject(ms);
      });
  });
};
/**
 * 登出
 * */
export const outLogin = function({ commit, state }) {
  return new Promise((resolve, reject) => {
    commit(types.SET_USER_ID, "");
    commit(types.SET_TOKEN, "");
    setUserId("");
    setToken("");
    resolve("ok");
  });
};
/**
 * 同步购物车
 * */
export const shoppingCat = function({ commit, state }) {
  return new Promise((resolve, reject) => {
    server
      .shoppingCat()
      .then(rs => {
        commit(types.SET_SHOPPING_CAT, rs.list);
        resolve("ok");
      })
      .catch(ms => {
        reject(ms);
      });
  });
};
/**
 * 添加购物车
 * */
export const addShoppingCat = function({ commit, state }, goods) {
  return new Promise((resolve, reject) => {
    let shoppingCat = clone(state.shoppingCat);
    let isGoods = ArrIsObject(goods, shoppingCat, "id");
    let list = [];
    if (!isGoods) {
      shoppingCat.unshift(goods);
      list = shoppingCat;
    } else {
      let p = clone(shoppingCat);
      list = p.map(item => {
        if (item.id === goods.id) {
          item.num++;
        }
        return item;
      });
    }
    commit(types.SET_SHOPPING_CAT, list);
    // 同步服务器和localStorage
    server
      .setShoppingCat(list)
      .then(rs => {
        resolve("ok");
      })
      .catch(ms => {
        reject(ms);
      });
  });
};
/**
 * 选择购物车中的商品
 * param goodsId 要选择Id ,-1全不选，0全选
 * */
export const selectShoppingCat = function({ commit, state }, goodsId) {
  return new Promise((resolve, reject) => {
    let shoppingCat = clone(state.shoppingCat);
    let list = [];
    if (goodsId !== undefined) {
      shoppingCat.forEach(item => {
        if (goodsId === item.id) {
          item.select = !item.select;
        } else if (goodsId === 0) {
          item.select = true;
        } else if (goodsId === -1) {
          item.select = false;
        }
        list.push(item);
      });
    }
    commit(types.SET_SHOPPING_CAT, list);
    // 同步服务器和localStorage
    server
      .setShoppingCat(list)
      .then(rs => {
        resolve("ok");
      })
      .catch(ms => {
        reject(ms);
      });
  });
};

/**
 * 删除、清空购物车
 * param goodsId 要删除Id ,不传则清空
 * param params.status 0：删除整个 1：减少数量为则删除
 * */
export const delShoppingCat = function({ commit, state }, params = null) {
  return new Promise((resolve, reject) => {
    let shoppingCat = clone(state.shoppingCat);
    let list = [];

    if (params !== null) {
      shoppingCat.forEach(item => {
        if (params.status === 0) {
          if (params.id !== item.id) {
            list.push(item);
          }
        } else {
          if (params.id === item.id) {
            item.num--;
          }
          if (item.num > 0) {
            list.push(item);
          }
        }
      });
    }
    commit(types.SET_SHOPPING_CAT, list);
    // 同步服务器和localStorage
    server
      .setShoppingCat(list)
      .then(rs => {
        resolve("ok");
      })
      .catch(ms => {
        reject(ms);
      });
  });
};
/**
 * 设置最近搜索
 *
 * @param type Number 1 服务器获取 2:添加 3:清空
 * @param val string 搜索词
 *
 * @param commit 系统传递
 * @param sate 系统传递
 * */
export const setRecentlyList = function(
  { commit, state },
  { val = "", type = 1 }
) {
  return new Promise((resolve, reject) => {
    //
    if (type === 1) {
      server
        .searchRecentlyList()
        .then(rs => {
          commit(types.SET_RECENTLY_LIST, rs.recentlyList);
          resolve();
        })
        .catch(ms => {
          reject(ms);
        });
    }
    //
    if (type === 2) {
      let recentlyList = state.recentlyList.slice();
      if (recentlyList.indexOf(val) === -1) {
        recentlyList.unshift(val);
        server.setSearchRecentlyList(val); // 同步服务器或localStorage
      }
      commit(types.SET_RECENTLY_LIST, recentlyList);
      resolve();
    }
    //
    if (type === 3) {
      commit(types.SET_RECENTLY_LIST, []);
      server.clearSearchRecentlyList(); // 同步服务器或localStorage
      resolve();
    }
  });
};
